<template>
    <div class="spuForm">
        <el-form label-width="98">
            <el-form-item label="SPU名称">
                <el-input placeholder="请你输入SPU名称" v-model="spuParams.spuName"></el-input>
            </el-form-item>
            <el-form-item label="SPU品牌">
                <el-select placeholder="请选择品牌" v-model="spuParams.tmId">
                    <el-option v-for="item in tradmarkList" :key="item.id" :label="item.tmName"
                        :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="SPU描述">
                <el-input type="textarea" placeholder="请输入品牌描述" v-model="spuParams.description"></el-input>
            </el-form-item>

            <el-form-item label="SPU照片">
                <el-upload v-model:file-list="imageList" action="/api/admin/product/fileUpload" list-type="picture-card"
                    :on-preview="handlePictureCardPreview">
                    <el-icon>
                        <Plus />
                    </el-icon>
                </el-upload>

                <el-dialog v-model="dialogVisible">
                    <img w-full :src="dialogImageUrl" alt="Preview Image" />
                </el-dialog>
            </el-form-item>
            <el-form-item label="SPU销售属性">
                <el-select style="width:160px;margin-right: 10px;margin-bottom: 10px" v-model="unSelectAttrArr">
                    <el-option v-for="item in unSelectSaleAttr" :key="item.id" :label="item.name"
                        :value="`${item.id}:${item.name}`"></el-option>
                </el-select>
                <el-button type="primary" icon="Plus" @click="addSaleAttr">添加销售属性</el-button>
                <el-table :data="saleAttr" border>
                    <el-table-column label="序号" type="index" align="center" width="100"></el-table-column>
                    <el-table-column label="属性名" prop="saleAttrName" width="180"></el-table-column>
                    <el-table-column label="属性值">
                        <template #default="scope">
                            <el-tag v-for="tag in scope.row.spuSaleAttrValueList" :key="tag.id"
                                @close="scope.row.spuSaleAttrValueList.splice(scope.$index, 1)" style="margin: 0 5px"
                                :closable="true">
                                {{ tag.saleAttrValueName }}
                            </el-tag>
                            <el-input v-model="scope.row.attrValueName" @blur="toShow(scope.row)"
                                v-if="scope.row.flag == true" style="width: 100px;" size="small"></el-input>
                            <el-button v-else type="primary" size="small" icon="Plus"
                                @click="addSaleAttrValueName(scope.row)"></el-button>

                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="180">
                        <template #default="scope">
                            <el-button type="danger" icon="delete" size="small"
                                @click="deleteAttrvalue(scope.$index)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="saveSpu">
                    保存
                </el-button>
                <el-button type="primary" @click="cancelAdd">
                    取消
                </el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue';
import { reqAllTradmarkList, reqSpuImageList, reqSpuSaleAttr, reqAllSpuSaleAttr, reqAddOrUpdateSpuInfo } from '@/api/product/spu/index'
import type { tradmarkInfoArr, spuImageDataArr, spuSaleAttrdataArr, HasSaleAttrArr, spuInfoData } from '@/api/product/spu/type'
import { ElMessage } from 'element-plus';

const emit = defineEmits(['changeScreen'])
const tradmarkList = ref<tradmarkInfoArr>([])
const imageList = ref<any>([])
const saleAttr = ref<spuSaleAttrdataArr>([])
const allSaleAttr = ref<HasSaleAttrArr>([])
const spuParams = ref<any>({
    category3Id: '',
    description: '',
    id: '',
    spuImageList: [],
    spuName: '',
    spuPosterList: [],
    spuSaleAttrList: [],
    tmId: ''
})
const cancelAdd = () => {
    emit('changeScreen', 0)
}
const getSpuInfo = async (row: any) => {
    spuParams.value = row
    const res = await reqAllTradmarkList()
    tradmarkList.value = res.data
    const res1 = await reqSpuImageList(row.id)
    imageList.value = res1.data.map((item) => {
        return {
            name: item.imgName,
            url: item.imgUrl
        }
    })
    const res2 = await reqSpuSaleAttr(row.id)
    saleAttr.value = res2.data
    const res3 = await reqAllSpuSaleAttr()
    allSaleAttr.value = res3.data
}
const dialogImageUrl = ref('')
const dialogVisible = ref()
const handlePictureCardPreview = (uploadFile: any) => {
    dialogImageUrl.value = uploadFile.url
    dialogVisible.value = true

}
const addSaleAttrValueName = (row: any) => {
    row.flag = true
}
const deleteAttrvalue = (index: number) => {
    saleAttr.value.splice(index, 1)
}
const unSelectAttrArr = ref<any>([])
const unSelectSaleAttr = computed(() => {
    const unSelect = allSaleAttr.value.filter((item: any) => {
        return saleAttr.value.every((item1: any) => {
            return item.name !== item1.saleAttrName
        })
    })
    return unSelect
})
const addSaleAttr = (row: any) => {
    const [baseSaleAttrId, saleAttrName] = unSelectAttrArr.value.split(":")
    saleAttr.value.push({
        baseSaleAttrId,
        saleAttrName,
        spuSaleAttrValueList: []
    })
    row.attrValueName = ''
}
const toShow = (row: any) => {
    const { baseSaleAttrId, attrValueName } = row
    if (attrValueName == '') {
        ElMessage({
            type: 'error',
            message: '属性值不能为空'
        })
        return
    }

    let repeat = row.spuSaleAttrValueList.find((item: any) => {
        return item.saleAttrValueName == attrValueName
    })
    if (repeat) {
        ElMessage({
            type: "error",
            message: "属性值重复"
        })
        return
    }
    row.spuSaleAttrValueList.push({
        baseSaleAttrId,
        saleAttrValueName: attrValueName
    })
    row.attrValueName = ''
    row.flag = false
}
const saveSpu = async () => {
    spuParams.value.spuImageList = imageList.value.map((item: any) => {
        return {
            imgName: item.name,
            imgUrl: item.response && item.response.data || item.url
        }
    })
    spuParams.value.spuSaleAttrList = saleAttr.value
    const res = await reqAddOrUpdateSpuInfo(spuParams)

}
defineExpose({
    getSpuInfo
})

</script>

<style lang="scss" scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>